package com.gaswell.utils;

import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Date;
import java.util.LinkedList;

public class JwtUtils {

    public static final String TOKEN_HEADER = "token";
    public static final String TOKEN_PREFIX = "Bearer ";

    private static final String SUBJECT = "xiaow";

    private static final long EXPIRITION = 1000 * 24 * 60 * 60 * 7;   //过期时间

    private static final String APPSECRET_KEY = "xiaow_secret";  //盐值

    private static final String ROLE_CLAIMS = "roles";



//    对输入的信息进行转换，这里的info就是用户id
        public static String generateAdminJsonWebToken(String info) {
        String token = Jwts
                .builder()
                .setSubject(SUBJECT)
                .claim("userid",info)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRITION))
                .signWith(SignatureAlgorithm.HS256, APPSECRET_KEY).compact();
        return token;
    }



    public static String generateAdminJsonWebToken(String userid,String departid) {
        String token = Jwts
                .builder()
                .setSubject(SUBJECT)
                .claim("userid",userid)
                .claim("departid",departid)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRITION))
                .signWith(SignatureAlgorithm.HS256, APPSECRET_KEY).compact();
        return token;
    }

    public static Claims checkJWT(String token) {
        try {
            final Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token).getBody();
            return claims;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取用户名
     *
     * @param token
     * @return
     */
    public static Claims getClaims(String token) {
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token).getBody();
        return claims;
    }

    public static String getUsernameWithoutSecert(String token) {
        String payload= token.split("\\.")[1];
        String payloaddecode = null;
        try {
            payloaddecode = new String(Base64.getDecoder().decode(payload.getBytes()), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = JSONObject.parseObject(payloaddecode);
        return jsonObject1.get("username").toString();
    }



    /**
     * 是否过期
     *
     * @param token
     * @return
     */
    public static boolean isExpiration(String token) {
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token).getBody();
        return claims.getExpiration().before(new Date());
    }

    public static void main(String[] args) {
        LinkedList<Integer> integers = new LinkedList<>();
        for(int i=0;i<150;i++){
            integers.add(null);
        }
        System.out.println(integers);
    }


}
